<template>
  <main class="post">
    <a-form-model ref="roleForm"
                  :rules="rules"
                  :label-col="labelCol"
                  :wrapper-col="wrapperCol" :model="form">
      <a-form-model-item label="所属科室">
        <a-select placeholder="请选择所属科室" :default-value="form.depTid" @change="selectChange">
          <a-select-option v-for="d in keshidata" :value="d.value">
            {{d.label}}
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="医生姓名" prop="doctorName">
        <a-input v-model="form.doctorName" placeholder="请输入角色名称"/>
      </a-form-model-item>
      <a-form-model-item label="年龄" prop="age">
        <a-input v-model="form.age" placeholder="请输入角色名称"/>
      </a-form-model-item>
      <a-form-model-item label="性别" prop="sex">
        <a-radio-group v-model="form.sex">
          <a-radio value="男">
            男
          </a-radio>
          <a-radio  value="女">
            女
          </a-radio>
          <a-radio  value="未知">
            未知
          </a-radio>
        </a-radio-group>
      </a-form-model-item>
      <a-form-model-item label="联系电话" prop="tel">
        <a-input v-model="form.tel" placeholder="请输入角色名称"/>
      </a-form-model-item>
    </a-form-model>
    <div class="footer">
      <a-row>
        <c-btn offset="18" span="3" type="primary" title="保存" @click="save"></c-btn>
        <c-btn span="3" type="default" title="取消" @click="$emit('update.visable',flase)"></c-btn>
      </a-row>
    </div>
  </main>
</template>

<script lang="js">
  import {getDeptList} from '@/api/common';
  import {add,update} from '@/api/doctor';
export default {
  props:{
    type:{
      default:'add'
    },
    editData:{
      default:{}
    },
  },
  data() {
    return {
      labelCol: {span: 4},
      wrapperCol: {span: 20},
      depTid:'',
      form: {
        doctorName: "",
        depTid:'',
        age:'',
        sex:'未知',
        tel:''
      },
      rules: {
        doctorName: [
          { required: true, message: '请输入医生名称', trigger: 'blur' },
        ],
        age: [
          { required: true, message: '请输入医生年龄', trigger: 'blur' },
        ],
        sex: [
          { required: true, message: '请选择医生性别', trigger: 'blur' },
        ],
        tel: [
          { required: true, message: '请输入医生电话', trigger: 'blur' },
        ],
      },
      treeData : [],
      expandedKeys: [],
      autoExpandParent: true,
      checkedKeys: [],
      keshidata:[]
    }
  },
  created() {
    console.log(this.type ,this.editData);
    if(this.type ==='edit'){
      this.getEditData()
    }
    this.handleSearch()
  },
  methods:{
    selectChange(value){
      console.log(value)
           this.form.depTid=value
    },
    async handleSearch(){
        const res = await getDeptList();
        if (res['data']) {
          this.keshidata=res.data.map((item)=>{
             return {label:item.depName,value: item.depTid}
          })
          console.log(this.keshidata)
        }
    },
    onExpand(expandedKeys) {
      console.log('onExpand', expandedKeys);
      this.expandedKeys = expandedKeys;
      this.autoExpandParent = false;
    },
    onCheck(checkedKeys) {
      console.log('onCheck', checkedKeys);
      this.checkedKeys = checkedKeys;
    },
    save(){
      this.$refs['roleForm'].validate((valid)=>{
        console.log(valid)
        if(valid){
          if(this.type==='add') this.setAddData();
          if(this.type ==='edit') this.setEditData()
        }else{
          this.$message.error('请完善表单信息')
        }
      })
    },
    async setAddData(){
      const params = {
        ...this.form
      }
      console.log(this.form)
      const res = await add(params);
      if(res['msg'] ==='ok'){
        this.$message.success('添加成功')
        this.$emit('update:visible',false)
        this.$emit('success',res)
      }else{
        this.$message.error(res.msg)
      }
    },
    async setEditData(){
      console.log(this.editData)
      const params = {
        ...this.form
      }
      console.log(this.form)
      const res = await update(params);
      if(res['msg'] ==='ok'){
        this.$message.success('编辑成功')
        this.$emit('update:visible',false)
        this.$emit('success',res)
      }else{
        this.$message.error(res.msg)
      }
    },
    getEditData(){
      this.form.age = this.editData.age;
      this.form.doctorCode = this.editData.doctorCode;
      this.form.doctorName = this.editData.doctorName;
      this.form.sex = this.editData.sex;
      this.form.tel = this.editData.tel;
      this.form.depTid=this.editData.depTid;
      this.form.dcTid=this.editData.dcTid
    }
  }
}
</script>

<style scoped lang="stylus">

</style>
